package cn.oracle.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 
 * @author wuyuhong
 * @date 2021/03/22 22:28
 * 这个配置类相当于spring-servlet.xml
 **/
@ComponentScan(basePackages = "cn.oracle",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class),@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Configuration.class)})
@Configuration
public class SpringMVCConfig extends WebMvcConfigurationSupport {
    @Override
    protected  void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/");
    }
    //jsp的配置
    @Override
    protected  void configureViewResolvers(ViewResolverRegistry registry){
        //路径，后缀
        registry.jsp("/jsp/",".jsp");
    }

    //路径映射
    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/hello3").setViewName("hello");
    }
//统一日期格式
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        ObjectMapper om=new ObjectMapper();
        //日期格式
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        MappingJackson2HttpMessageConverter converte=new MappingJackson2HttpMessageConverter(om);
        converters.add(converte);
    }
}
